using System.Web.Mvc;
using Wikey.Domain.Components;
using Wikey.Entity;
using Wikey.Domain.Repository;

namespace Wikey.Web.Mvc.Controllers
{
    public class SpaceController : Controller
    {
        public SpaceController()
        {
            repository = Factory.Get<ISpaceRepository>();
        }

        private readonly ISpaceRepository repository; 

        public ActionResult Index()
        {
            ViewData.Model = repository.SelectAll();
            return View();
        }

        public ActionResult Create()
        {

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public JavaScriptResult Create( FormCollection collection )
        {
            var space = new Space {Id = 0 , Name = collection["Name"],Description=collection["Description"]};
            repository.Save(space);
            
            var jScript = new JavaScriptResult {Script = "Saved successfully"};

            return jScript;
        }

        public ActionResult Look( string spaceName )
        {
            var space = repository.Find(spaceName);

            ViewData.Model = space;

            return View();
        }

    }
}
